package com.eques.demo.adapter

/**
* @description
* @author Create by yc on 2025/10/29/Wed 13:20
*/
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.eques.bean.LockMessage
import com.eques.demo.R

class LockMessageAdapter : RecyclerView.Adapter<LockMessageAdapter.ViewHolder>() {

    private var lockMessages: List<LockMessage> = emptyList()

    // 点击监听器
    var onItemClickListener: ((LockMessage) -> Unit)? = null

    fun setData(messages: List<LockMessage>) {
        this.lockMessages = messages
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_lock_message, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val message = lockMessages[position]
        holder.bind(message)

        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(message)
        }
    }

    override fun getItemCount(): Int = lockMessages.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivMessageIcon: ImageView = itemView.findViewById(R.id.iv_message_icon)
        private val tvMessageType: TextView = itemView.findViewById(R.id.tv_message_type)
        private val tvCreateTime: TextView = itemView.findViewById(R.id.tv_create_time)
        private val tvMessageState: TextView = itemView.findViewById(R.id.tv_message_state)
        private val tvDeviceId: TextView = itemView.findViewById(R.id.tv_device_id)
        private val viewUnreadDot: View = itemView.findViewById(R.id.view_unread_dot)

        fun bind(message: LockMessage) {
            // 设置消息图标
//            ivMessageIcon.setImageResource(message.getMessageIcon())

            // 设置消息类型
            tvMessageType.text = message.getMessageType()

            // 设置创建时间
            tvCreateTime.text = message.getFormattedTime()

            // 设置消息状态
//            tvMessageState.text = "状态: ${message.getMessageState()}"

            // 设置设备ID
            tvDeviceId.text = "设备ID: ${message.lock_id.take(8)}..."

            // 设置未读状态
//            if (message.isUnread()) {
//                viewUnreadDot.visibility = View.VISIBLE
//                itemView.setBackgroundColor(0xFFF5F9FF.toInt()) // 未读消息背景色
//            } else {
//                viewUnreadDot.visibility = View.GONE
//                itemView.setBackgroundColor(0xFFFFFFFF.toInt()) // 已读消息背景色
//            }

            // 根据消息类型设置不同的颜色
            setMessageTypeColor(message.type)
        }

        private fun setMessageTypeColor(messageType: Int) {
            val color = when (messageType) {
                0 -> "#2196F3" // 系统通知 - 蓝色
                1 -> "#4CAF50" // 开锁提醒 - 绿色
                2 -> "#F44336" // 报警通知 - 红色
                3 -> "#FF9800" // 设备状态 - 橙色
                4 -> "#9C27B0" // 固件更新 - 紫色
                5 -> "#FFC107" // 电量提醒 - 黄色
                6 -> "#607D8B" // 网络状态 - 灰色
                else -> "#757575" // 默认 - 深灰
            }
            tvMessageType.setTextColor(android.graphics.Color.parseColor(color))
        }
    }
}